let mongoose = require("./db.js");

//用户表
// let userSchema = new mongoose.Schema({
//   username: String,
//   password: String,
//   phone: String,
//   face_id: {
//     type: String,
//     default: ''
//   },
//   avatar: {
//     type: String, // 头像类型为字符串
//     default: "", // 头像字段默认值为空字符串
//   },
//   status: {
//     type: Number, // 状态类型为数字
//     default: 0, // 状态字段默认值为0
//   },
//   lastLoginTime: {
//     type: Date, // 最后登录时间类型为日期
//     default: Date.now, // 最后登录时间字段默认值为当前日期和时间
//   },
//   lastActiveTime: {
//     type: Date, // 最后活跃时间类型为日期
//     default: Date.now, // 最后活跃时间字段默认值为当前日期和时间
//   },
//   // 定义好友列表字段
//   friends: [{ type: mongoose.Schema.Types.ObjectId, ref: "users" }], // 定义好友列表为 ObjectId 类型并引用 user 表
//   // 定义加入的群列表字段
//   groups: [{ type: mongoose.Schema.Types.ObjectId, ref: "group" }], // 定义加入的群列表为 ObjectId 类型并引用 group 表
// })

let userSchema = new mongoose.Schema({
  username: String,
  password: String,
  phone: String,
  face_id: {
    type: String,
    default: "",
  },
  avatar: {
    type: String, // 头像类型为字符串
    default: "", // 头像字段默认值为空字符串
  },
  status: {
    type: Number, // 状态类型为数字
    default: 0, // 状态字段默认值为0
  },
  lastLoginTime: {
    type: Date, // 最后登录时间类型为日期
    default: Date.now, // 最后登录时间字段默认值为当前日期和时间
  },
  lastActiveTime: {
    type: Date, // 最后活跃时间类型为日期
    default: Date.now, // 最后活跃时间字段默认值为当前日期和时间
  },
  type: {
    type: String,
    default: "user",
  },
  // 定义好友列表字段
  friends: [{ type: mongoose.Schema.Types.ObjectId, ref: "user" }], // 定义好友列表为 ObjectId 类型并引用 user 表
  role: {
    type: mongoose.Schema.Types.ObjectId, //角色id
    ref: "role",
  },
});

// 刘鑫
//楼栋分类表
let dongSchema = new mongoose.Schema({
    name: String,
    s: Number,
});

//房间关联楼栋表
let roomSchema = new mongoose.Schema({
    name: String,
    cid: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "dong",
    },
    userid: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "user",
    },
});

//缴费分类表
let paySchema = new mongoose.Schema({
    name: String,
    price: Number,
    computer: String,
});

//缴费记录表关联分类表 用户表 房间表
let paylistSchema = new mongoose.Schema({
  rommid: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "room",
  },
  cid: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "pay",
  },
  userid: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "user",
  },
  time: Array,
  status: Boolean,
  title: String,
  payable: Number, //应缴
});

//交完费用记录表
let paylist1Schema = new mongoose.Schema({
  name: String,
  time: String,
  price: Number,
  title: String,
});
// 访客表
let visitorSchema = new mongoose.Schema({
  name: String, // 访客姓名
  gender: String, // 访客性别
  phone: String, // 访客电话
  time: String, // 访客时间
  status: Boolean, // 访客状态
  rommid: String, // 被访房间
  userid: String, // 被访屋主
  // roomid: {         // 被访房间
  //     type: mongoose.Schema.Types.String,
  //     ref: 'room'
  // },

  // userid: {         // 被访屋主
  //     type: mongoose.Schema.Types.String,
  //     ref: 'user'
  // },
  duration: String, // 访问时长
  img: String, //通行证
});
// 刘玉翠
// 公告表
let noticeSchema = new mongoose.Schema({
  title: String,
  content: String,
  time: {
    type: Date, // 消息发送时间类型为日期
    default: Date.now, // 消息发送时间默认值为当前日期和时间
  },
  status: Boolean,
  userid: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "user",
  },
});

// 房屋租赁
let houserentSchema = new mongoose.Schema({
    cate: String,//整租0、合租1
    title: String,// 房屋标题
    price: Number, // 房屋价格
    area: Number, // 房屋面积
    desc: String, // 房屋描述
    facility: Array, // 房屋设施
    img: Array, // 房屋图片
    time: String, // 发布时间
    userid: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "user",
    },
    roomid: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "room"
    },
})

// 李航 聊天记录
const messageModel = mongoose.model(
    "message",
    {
        type: {
            type: String, // 消息类型类型为字符串
            required: true, // 消息类型是必填项
        },
        content: {
            type: String, // 消息内容类型为字符串
            default: "", // 消息内容字段默认值为空字符串
        },
        // 定义表情字段
        emoji: {
            type: String, // 表情类型为字符串
            default: "",
        },
        userId: {
            type: mongoose.Schema.Types.ObjectId, // 消息来源用户 ID 类型为 ObjectId
            ref: "user", // 消息来源用户 ID 引用 user 表
        },
        friendId: {
            type: mongoose.Schema.Types.ObjectId, // 消息接收用户 ID 类型为 ObjectId
            ref: "user", // 消息接收用户 ID 引用 user 表
        },
        group: {
            type: mongoose.Schema.Types.ObjectId, // 消息接收群 ID 类型为 ObjectId
            ref: "group", // 消息接收群 ID 引用 group 表
        },
        time: {
            type: Date, // 消息发送时间类型为日期
            default: Date.now, // 消息发送时间默认值为当前日期和时间
        }
    },
  "message"
);

// 王笑雨
// 权限表
const permissionSchema = mongoose.Schema({
    name: String, //权限名称
    router_name: String, //路由名称
    parentid: {
        type: mongoose.Schema.Types.ObjectId, //父级id
    },
    level: Number, //几级权限
});

// wxy角色表
const roleSchema = mongoose.Schema({
    name: String, //角色名称
    permission: Array, //权限
});

//jwh我的房屋表
const myroomSchema = new mongoose.Schema(
  {
    ne: String, //小区
    unit: String, //单元
    owner: String, //业主
    status: String, //状态
    auth_status: {
      type: String,
      default: 1,
    }, //认证状态 1:认证中 2:认证成功 3:认证失败
    def_Status: {
      type: Number,
      default: 0,
    }, //默认状态 1:默认 0:非默认
  },
  { versionKey: false }
);

//jwh的业主信息表
const ownerInformationSchema = new mongoose.Schema({}, { versionKey: false });

let maint_jd = new mongoose.Schema(
  {
    title: String,
  },
  { versionKey: false }
);

let maintSchema = new mongoose.Schema({
  address: String,
  title: String,
  content: String,
  time: {
    type: Date,
    default: Date.now,
  },
  tel: String,
  type: String,
  status: {
    type: Boolean,
    default: false,
  },
  address: String,
  userId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "user",
  },
});


let user = mongoose.model("user", userSchema, "user");
let dong = mongoose.model("dong", dongSchema, "dong");
let room = mongoose.model("room", roomSchema, "room");
let pay = mongoose.model("pay", paySchema, "pay");
let notice = mongoose.model("notice", noticeSchema, "notice");
let paylist = mongoose.model("paylist", paylistSchema, "paylist");
let visitor = mongoose.model("visitor", visitorSchema, "visitor");
let houserent = mongoose.model("houserent", houserentSchema, "houserent");
let myroomModel = mongoose.model("myroom", myroomSchema, "myroom");
let NoticeModel = mongoose.model("notice", noticeSchema, "notice");
const roleModel = mongoose.model("role", roleSchema);
const permissionModel = mongoose.model("permission", permissionSchema);
let maint_jdModel = mongoose.model("maint_jd", maint_jd, "maint_jd");
let maintModel = mongoose.model("maint", maintSchema, "maint");
let paylist1 = mongoose.model("paylist1", paylist1Schema, "paylist1");

module.exports = {
  user,
  paylist1,
  dong,
  room,
  pay,
  notice,
  paylist,
  visitor,
  myroomModel,
  NoticeModel,
  roleModel,
  permissionModel,
  houserent,
  messageModel,
  maint_jdModel,
  maintModel,
};
